<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动名称" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参与范围" prop="participantScope">
        <el-input
          v-model="queryParams.participantScope"
          placeholder="请输入参与范围"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动等级" prop="activityLevel">
        <!-- 活动等级选择器，绑定查询参数中的活动等级字段 -->
        <!-- v-model 用于双向绑定查询参数中的 activityLevel 属性 -->
        <!-- placeholder 为选择器未选择时显示的提示文本 -->
        <!-- clearable 属性允许用户清除已选内容 -->
        <!-- collapse-tags-tooltip 属性在标签过多时显示工具提示 -->
        <!-- style 设置选择器的最小宽度为 150px -->
        <el-select
          v-model="queryParams.activityLevel"
          placeholder="请选择活动等级"
          clearable
          collapse-tags-tooltip
          style="min-width: 150px"
        >
          <el-option
            v-for="dict in activity_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学分类型" prop="creditType">
        <el-select
          v-model="queryParams.creditType"
          placeholder="请选择学分类型"
          clearable
          collapse-tags-tooltip
          style="min-width: 150px"
        >
          <el-option
            v-for="dict in credit_value"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择活动状态"
          clearable
          collapse-tags-tooltip
          style="min-width: 150px"
        >
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wlxz:activity:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wlxz:activity:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wlxz:activity:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wlxz:activity:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="activityList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--  <el-table-column label="活动id" align="center" prop="activityId" /> -->
      <el-table-column label="名称" align="center" prop="activityName" />
      <!-- <el-table-column label="活动描述" align="center" prop="activityDes" /> -->
      <!-- <el-table-column label="活动要求" align="center" prop="requirements" /> -->
      <el-table-column label="地点" align="center" prop="activityLocation" />
      <el-table-column label="参与范围" align="center" prop="participantScope" />
      <el-table-column label="人数" align="center" prop="participantLimit" />
      <el-table-column label="等级" align="center" prop="activityLevel">
        <template #default="scope">
          <dict-tag :options="activity_level" :value="scope.row.activityLevel" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="creditType">
        <template #default="scope">
          <dict-tag :options="credit_value" :value="scope.row.creditType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="发放分值" align="center" prop="creditValue" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="报名开始"
        align="center"
        prop="registrationStart"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.registrationStart, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="报名结束"
        align="center"
        prop="registrationEnd"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.registrationEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="活动开始" align="center" prop="activityStart" width="120">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityStart, "{y}-{m}-{d}-{h}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束" align="center" prop="activityEnd" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activityEnd, "{y}-{m}-{d}-{h}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="responsiblePerson" />
      <!-- <el-table-column label="负责人联系方式" align="center" prop="responsibleContact" />
      <el-table-column label="负责导师" align="center" prop="supervisor" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wlxz:activity:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wlxz:activity:remove']"
            >删除</el-button
          >
           <el-button
            link
            type="success"
            icon="el-icon-user"
            @click="handleClick(scope.row)"
            >查看成员</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件，当总记录数大于 0 时显示 -->
    <!-- v-show="total > 0"：控制分页组件的显示与隐藏，仅在总记录数 total 大于 0 时显示 -->
    <!-- :total="total"：将总记录数 total 绑定到分页组件，用于显示总记录数信息 -->
    <!-- v-model:page="queryParams.pageNum"：双向绑定当前页码，将分页组件的当前页码同步到 queryParams.pageNum -->
    <!-- v-model:limit="queryParams.pageSize"：双向绑定每页显示的记录数，将分页组件的每页条数同步到 queryParams.pageSize -->
    <!-- @pagination="getList"：监听分页事件，当页码或每页条数改变时，调用 getList 方法重新获取数据 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动发布对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-tabs v-model="activeTab">
        <el-tab-pane label="活动信息" name="activity">
          <el-form ref="activityRef" :model="form" :rules="rules" label-width="120px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="活动名称" prop="activityName" required>
                  <el-input v-model="form.activityName" placeholder="请输入活动名称" />
                </el-form-item>
                <el-form-item label="活动地点" prop="activityLocation" required>
                  <el-input
                    v-model="form.activityLocation"
                    placeholder="请输入活动地点"
                  />
                </el-form-item>
                <el-form-item label="参与范围" prop="participantScope">
                  <el-input
                    v-model="form.participantScope"
                    placeholder="请输入参与范围"
                  />
                </el-form-item>
                <el-form-item label="人数限制" prop="participantLimit" required>
                  <el-input
                    v-model="form.participantLimit"
                    placeholder="请输入人数限制"
                  />
                </el-form-item>
                <el-form-item label="活动等级" prop="activityLevel" required>
                  <el-select v-model="form.activityLevel" placeholder="请选择活动等级">
                    <el-option
                      v-for="dict in activity_level"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="学分类型" prop="creditType" required>
                  <el-select v-model="form.creditType" placeholder="请选择学分类型">
                    <el-option
                      v-for="dict in credit_value"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="发放分值" prop="creditValue">
                  <el-input v-model="form.creditValue" placeholder="请输入发放分值" />
                </el-form-item>
                <el-form-item label="活动状态" prop="status">
                  <el-select v-model="form.status" placeholder="请选择活动状态">
                    <el-option
                      v-for="dict in status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="活动描述" prop="activityDes" required>
                  <el-input
                    v-model="form.activityDes"
                    type="textarea"
                    placeholder="请输入内容"
                  />
                </el-form-item>
                <el-form-item label="活动要求" prop="requirements">
                  <el-input
                    v-model="form.requirements"
                    type="textarea"
                    placeholder="请输入内容"
                  />
                </el-form-item>

                <el-form-item label="报名开始时间" prop="registrationStart">
                  <el-date-picker
                    clearable
                    v-model="form.registrationStart"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm"
                    placeholder="请选择报名开始时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="报名结束时间" prop="registrationEnd">
                  <el-date-picker
                    clearable
                    v-model="form.registrationEnd"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm"
                    placeholder="请选择报名结束时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="活动开始时间" prop="activityStart">
                  <el-date-picker
                    clearable
                    v-model="form.activityStart"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm"
                    placeholder="请选择活动开始时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="活动结束时间" prop="activityEnd">
                  <el-date-picker
                    clearable
                    v-model="form.activityEnd"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm"
                    placeholder="请选择活动结束时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="负责人" prop="responsiblePerson">
                  <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
                </el-form-item>
                <el-form-item label="负责人联系方式" prop="responsibleContact">
                  <el-input
                    v-model="form.responsibleContact"
                    placeholder="请输入负责人联系方式"
                  />
                </el-form-item>
                <el-form-item label="负责导师" prop="supervisor">
                  <el-input v-model="form.supervisor" placeholder="请输入负责导师" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="学分补录" name="supplement">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                v-if="wxzSupplementRecordList.length === 0"
                type="primary"
                icon="Plus"
                @click="handleAddWxzSupplementRecord"
                >添加</el-button
              >
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button
                type="danger"
                icon="Delete"
                @click="handleDeleteWxzSupplementRecord"
                >删除</el-button
              >
            </el-col> -->
          </el-row>
          <el-form
            v-for="(item, index) in wxzSupplementRecordList"
            :key="index"
            label-width="120px"
            class="supplement-form"
          >
            <el-divider content-position="left">补录学分基础信息</el-divider>
            <el-form-item label="活动开始时间">
              <el-date-picker
                clearable
                v-model="item.startTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择活动开始时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                clearable
                v-model="item.endTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择结束时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="补录内容">
              <el-input v-model="item.content" placeholder="请输入补录内容" />
            </el-form-item>
            <el-form-item label="活动一级分类">
              <el-input v-model="item.firstCategory" placeholder="请输入活动一级分类" />
            </el-form-item>
            <el-form-item label="活动二级分类">
              <el-input v-model="item.secondVategory" placeholder="请输入活动二级分类" />
            </el-form-item>
            <el-form-item label="活动等级">
              <el-select
                v-model="item.activityLevel"
                placeholder="请选择活动等级"
                :value="form.creditType"
              >
                <el-option
                  v-for="dict in activity_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学分类型">
              <el-select v-model="item.creditType" placeholder="请选择学分类型">
                <el-option
                  v-for="dict in credit_value"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="发放分值">
              <el-input v-model="item.creditValue" placeholder="请输入发放分值" />
            </el-form-item>
            <!-- <el-checkbox v-model="item.selected" style="margin-left: 120px;">选择此条记录</el-checkbox> -->
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Activity">
import {
  listActivity,
  getActivity,
  delActivity,
  addActivity,
  updateActivity,
} from "@/api/wlxz/activity";
import { useActivityStore } from "@/store/modules/wlxz/activity";
const activityStore = useActivityStore();

import { useRouter } from "vue-router";
const router = useRouter();
const handleClick = (row) => {
  activityStore.setActivityId(row.activityId);
  console.log("点击了跳转了");
  router.push("/activitymanagement/stupublic");
};

const { proxy } = getCurrentInstance();
const { activity_level, status, credit_value } = proxy.useDict(
  "activity_level",
  "status",
  "credit_value"
);

const activityList = ref([]);
const wxzSupplementRecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedWxzSupplementRecord = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityName: null,
    activityDes: null,
    requirements: null,
    activityLocation: null,
    participantScope: null,
    participantLimit: null,
    activityLevel: null,
    creditType: null,
    creditValue: null,
    status: null,
    registrationStart: null,
    registrationEnd: null,
    activityStart: null,
    activityEnd: null,
    responsiblePerson: null,
    responsibleContact: null,
    supervisor: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询活动发布列表 */
function getList() {
  loading.value = true;
  listActivity(queryParams.value).then((response) => {
    activityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    activityId: null,
    activityName: null,
    activityDes: null,
    requirements: null,
    activityLocation: null,
    participantScope: null,
    participantLimit: null,
    activityLevel: null,
    creditType: null,
    creditValue: null,
    status: null,
    registrationStart: null,
    registrationEnd: null,
    activityStart: null,
    activityEnd: null,
    responsiblePerson: null,
    responsibleContact: null,
    supervisor: null,
    createBy: null,
    createTime: null,
  };
  wxzSupplementRecordList.value = [];
  proxy.resetForm("activityRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.activityId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加活动发布";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _activityId = row.activityId || ids.value;
  getActivity(_activityId).then((response) => {
    form.value = response.data;
    wxzSupplementRecordList.value = response.data.wxzSupplementRecordList;
    open.value = true;
    title.value = "修改活动发布";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityRef"].validate((valid) => {
    if (valid) {
      form.value.wxzSupplementRecordList = wxzSupplementRecordList.value;
      if (form.value.activityId != null) {
        updateActivity(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addActivity(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _activityIds = row.activityId || ids.value;
  proxy.$modal
    .confirm('是否确认删除活动发布编号为"' + _activityIds + '"的数据项？')
    .then(function () {
      return delActivity(_activityIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 学分补录添加按钮操作 */
function handleAddWxzSupplementRecord() {
  let obj = {};
  obj.startTime = "";
  obj.endTime = "";
  obj.content = "";
  obj.firstCategory = "";
  obj.secondVategory = "";
  obj.activityLevel = "";
  obj.awardContent = "";
  obj.creditType = "";
  obj.creditValue = "";
  wxzSupplementRecordList.value.push(obj);
}

/** 学分补录删除按钮操作 */
function handleDeleteWxzSupplementRecord() {
  if (checkedWxzSupplementRecord.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的学分补录数据");
  } else {
    const wxzSupplementRecords = wxzSupplementRecordList.value;
    const checkedWxzSupplementRecords = checkedWxzSupplementRecord.value;
    wxzSupplementRecordList.value = wxzSupplementRecords.filter(function (item) {
      return checkedWxzSupplementRecords.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleWxzSupplementRecordSelectionChange(selection) {
  checkedWxzSupplementRecord.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "wlxz/activity/export",
    {
      ...queryParams.value,
    },
    `activity_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
